package com.lg.delay.queue.task;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * 任务执行管理
 *
 * @author by Mr. Li 2020/12/9 17:41
 */
@Slf4j
public class TaskManager {
    public TaskManager() {
    }

    /**
     * 创建一个可重用的固定线程数的线程池
     */
    private static ExecutorService executorService = Executors.newFixedThreadPool(10);

    /**
     * 异步执行任务
     *
     * @param task  任务
     * @param title 标题
     */
    public static void doTask(final ITask task, String title) {
        executorService.execute(() -> {
            SpeedTimeLogSuit.wrap(() -> {
                try {
                    task.doTask();
                } catch (Exception e) {
                    log.error("TaskManager doTask execute error.", e);
                }
                return null;
            }, title);
        });
    }

    /**
     * 带有返回值的task
     *
     * @param task  任务
     * @param title 标题
     */
    public static <T> FutureTask<T> doFutureTask(final IFutureTask<T> task, String title) {
        FutureTask<T> future = new FutureTask<>(() -> SpeedTimeLogSuit.wrap((task::doTask), title));
        executorService.execute(future);
        return future;
    }

}
